package sketchpad.business.function.impl;

import sketchpad.business.datacalc.calchelper.CalcHelperService;
import sketchpad.business.datacalc.calchelper.impl.CalcHelperServiceImpl;
import sketchpad.constant.Calculate;
import sketchpad.constant.Config;
import sketchpad.gui.control.DrawControl;
import sketchpad.gui.drawinggraph.DrawItem;
import sketchpad.gui.drawinggraph.node.Node;
import sketchpad.business.function.OperateService;

import java.util.List;
import java.util.Map;

public final class OperateServiceImpl implements OperateService {

    private DrawControl control;

    public OperateServiceImpl(DrawControl drawControl){
        control = drawControl;
    }

    //计算操作时点移动方向与距离，以便修改选中图形的所有点的坐标
    private double lastX = Config.SELECT_ID;
    private double lastY = Config.SELECT_ID;

    private void moveItem(Node node){
        if (Math.abs(node.x - lastX)> Calculate.MOUSE_DRAGGED_DYNAMIC_RADIUS || Math.abs(node.y -lastY)>Calculate.MOUSE_DRAGGED_DYNAMIC_RADIUS){
            lastX = node.x;
            lastY = node.y;
        }
        double deltX = node.x - lastX;
        double deltY = node.y - lastY;
        lastX = node.x;
        lastY = node.y;
        List<Integer> selectedList = control.getSelectedItemList();
        Map<Integer, DrawItem> drawItemMap = control.getDrawItemMap();
        CalcHelperService calcHelperService = new CalcHelperServiceImpl(control);
        for (Integer i : selectedList){
            calcHelperService.modifyNewCor(drawItemMap.get(i),deltX,deltY);
        }
    }

    @Override
    public void move(Node mouseNode) {
        moveItem(mouseNode);
    }

    @Override
    public void copy() {
        control.getBufferPoolServiceImpl().add();
    }

    @Override
    public void paste() {
        control.getBufferPoolServiceImpl().restore();
        control.drawAllItem();
    }
}
